package com.blockchain.console.servlets.financial.tx;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.enums.WithdrawStatus;
import com.blockchain.console.service.financial.TxglManage;
import com.blockchain.console.service.financial.entity.TxglRecord;
import com.blockchain.console.servlets.financial.AbstractFinancialServlet;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.util.parser.IntegerParser;

@Right(id = "P2P_C_FINANCE_CHECKTXGL", name = "用户提现-提现管理审核")
public class Check extends AbstractFinancialServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request,
			HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		TxglManage extraction = serviceSession.getService(TxglManage.class);
		int id = IntegerParser.parse(request.getParameter("id"));
		TxglRecord txglRecord = extraction.get(id);
		request.setAttribute("txglRecord", txglRecord);
		super.processGet(request, response, serviceSession);
	}

	@Override
	protected void processPost(final HttpServletRequest request,
			final HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		TxglManage extraction = serviceSession.getService(TxglManage.class);
		int id = IntegerParser.parse(request.getParameter("id"));
		String check_reason = request.getParameter("check_reason");
		WithdrawStatus status = EnumParser.parse(WithdrawStatus.class,
				request.getParameter("status"));
		try {
			extraction.check(status, check_reason, id);
			if (status == WithdrawStatus.SHSB) {
				sendRedirect(request, response,
						getController().getURI(request, Shsb.class));
			} else if (status == WithdrawStatus.SHTG) {
				sendRedirect(request, response,
						getController().getURI(request, Shtg.class));
			}
		} catch (Throwable throwable) {
			if (throwable instanceof ParameterException
					|| throwable instanceof LogicalException) {
				getController().prompt(request, response, PromptLevel.WARRING,
						throwable.getMessage());
				processGet(request, response, serviceSession);
			} else {
				super.onThrowable(request, response, throwable);
			}
		}
	}
}
